package com.ziyia.databasetest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListAdapter;

public class MainActivity extends AppCompatActivity {
    private final Integer NEW_VERSION = 3;

    private Button createDatabase, insert, update, delete, select, replaceData;

    private MyDatabaseHelper myDatabaseHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        createDatabase = findViewById(R.id.createDatabase);
        insert = findViewById(R.id.insert);
        update = findViewById(R.id.update);
        delete = findViewById(R.id.delet);
        select = findViewById(R.id.select);
        replaceData = findViewById(R.id.replaceData);

        myDatabaseHelper = new MyDatabaseHelper(this, "BookStrore.db", null, NEW_VERSION);
        createDatabase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myDatabaseHelper.getWritableDatabase();
            }
        });

        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert();
            }
        });

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                update();
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete();
            }
        });

        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                select();
            }
        });


        replaceData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                transaction();
            }
        });
    }

    private void transaction() {
        SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();
        // 开启事务
        db.beginTransaction();
        try {
            db.delete("Book", null, null);
            if (true) {
                throw new NullPointerException();
            }
            ContentValues contentValues = new ContentValues();
            contentValues.put("name", "Game of Thrones");
            contentValues.put("author","George Martin");
            contentValues.put("pages", 500);
            contentValues.put("price", 99.9);

            // 事务已经执行成功。
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 结束事务
            db.endTransaction();
        }

    }

    private void insert() {
        SQLiteDatabase writableDatabase = myDatabaseHelper.getWritableDatabase();

        // 组装第一条数据
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", "图解HTTP");
        contentValues.put("author", "Ban Brown");
        contentValues.put("pages", 400);
        contentValues.put("price", 16.96);
        // 插入第一条数据
        writableDatabase.insert("Book",null, contentValues);


        contentValues.clear();

        // 组装第二条数据
        contentValues.put("name", "Java编程思想");
        contentValues.put("author", "Ban Brown");
        contentValues.put("pages", 700);
        contentValues.put("price", 96.96);

        // 插入第二条数据
        writableDatabase.insert("Book",null, contentValues);
    }

    private void update() {
        SQLiteDatabase writableDatabase = myDatabaseHelper.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put("price", 1000);

        //
        writableDatabase.update("Book", contentValues, "name = ?", new String[]{"图解HTTP"});

    }


    private void select () {
        SQLiteDatabase writableDatabase = myDatabaseHelper.getWritableDatabase();
        Cursor book = writableDatabase.query("Book", null, null, null, null, null, null);
//        while (book.moveToFirst()) {
//            Log.d("select", book.toString());
//        }

        if (book.moveToFirst()) {
            do {
                String name = book.getString(book.getColumnIndex("name"));
                Log.d("select", name);
            } while (book.moveToNext());
        }
    }

    private void delete() {
        SQLiteDatabase writableDatabase = myDatabaseHelper.getWritableDatabase();
        writableDatabase.delete("Book", "pages > ?", new String[]{"500"});
    }
}